package com.xf.day04
import org.apache.spark.{SparkContext,SparkConf}

object Dsf {
  def main(args: Array[String]): Unit = {

    // 设置 JVM 参数来解决模块访问问题

    val conf = new SparkConf().setAppName("student").setMaster("local")

    val sc = new SparkContext(conf)

    sc.setLogLevel("ERROR")

    val stuRDD = sc.textFile("D:\\workRecord\\spark_project20250924\\data\\student.txt")

    val stutTuple = stuRDD.map{line => val split = line.split(" ");(split(0), split(1), split(2), split(3), split(4), split(5))}

    val nums = stutTuple.map(_._2).distinct().count()-1
    println("The number of participants is"+ nums)

    val nums20 = stutTuple.map(x=> (x._2,x._3)).distinct().filter(_._2=="20").count()
    println("The number of participants who are 20 years old is"+ nums20)

    val numsless21 = stutTuple.map(x=> (x._2,x._3)).distinct().filter(_._2<"21").count()
    println("The number of participants who are less than 21 years old is"+ numsless21)

    val numMale= stutTuple.map(x=> (x._2,x._4)).distinct().filter(_._2=="Male").count()
    println("The number of participants who are male is"+ numMale)

    val nums12 = stutTuple.map(x=> (x._1,x._2)).distinct().filter(_._1=="12").count()
    print("The number of participants who are 12 years old is"+ nums12)

    val meanChinese = stutTuple.filter(x=> (x._5=="Chinese")).map(_._6.toDouble).mean()
    println("The mean score of Chinese is"+ meanChinese.toString)

    val sumStu12 = stutTuple.filter(x=> (x._1=="12")).distinct().map(x=> (x._1,x._6.toDouble)).reduceByKey((a,b)=> a+b).collect().toBuffer
    println("The sum of score of 12 years old is"+ sumStu12)

    val meanClass = stutTuple.filter(_._1=="12").map(x=> (x._1,x._6.toDouble)).groupByKey().mapValues(x=> (x.sum/x.size)*3).collect().toBuffer
    println("The mean score of 12 years old is"+ meanClass)

    val topChinese = stutTuple.filter(_._5=="Chinese").map(x=> (x._2,x._6.toDouble)).sortBy(_._2,false).take(1)
    println("The top score of Chinese is"+ topChinese.toBuffer)

    val num14 = stutTuple.filter(_._1=="14").map(x=> (x._2,x._6.toDouble)).groupByKey().mapValues(_.sum).filter(_._2>190).count()
    println("The number of participants who are 14 years old and score more than 190 is"+ num14)
    sc.stop()
  }
}
